//动态说说路由
var express = require('express');
var router = express.Router();
var { dynamic, user } = require('../dao/index.js');
//上传说说
router.post('/updata', function (req,res,next) {
    dynamic.create(req.body, function (err, data) {
        if (err) {
            res.json({
                status: false,
                err:'发送失败，请稍后再试'
            })
        } else {
            res.json({
                status: true,
                lay: '发送成功'
            })
        }
    })
})

//查询说说
router.get('/data', async function (req, res, next) {
    if (req.query.id) {
        var info = await dynamic.find({ use_id: req.query.id }).sort({ 'time': -1 })
        var data = await user.findById(req.query.id, { name: 1, head: 1 })
        for (var index in info) {
            info[index] = {
                ues: data,
                data: info[index]
            }
        }
        res.json(info)
    } else {
        var info = await dynamic.find({}).sort({ 'time': -1 })
        for (var index in info) {
            var data = await user.findById(info[index].use_id, { name: 1, head: 1 })
            info[index] = {
                ues: data,
                data: info[index]
            }
        }
        res.json(info)
    }
    
})
module.exports = router